import Vue from 'vue';
import ElementUI from 'element-ui';
import VCharts from 'v-charts';
import ECharts from 'vue-echarts';
import axios from 'axios';
import VueAxios from 'vue-axios';

import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
import { fab } from '@fortawesome/free-brands-svg-icons';
import {
  FontAwesomeIcon,
  FontAwesomeLayers,
  FontAwesomeLayersText,
} from '@fortawesome/vue-fontawesome';
import App from './App.vue';
import router from './router';
import store from './store';
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false;

Vue.use(ElementUI);
Vue.use(VCharts);
Vue.use(VueAxios, axios);

library.add(fas);
library.add(fab);

Vue.component('v-chart', ECharts);
Vue.component('font-awesome-icon', FontAwesomeIcon);
Vue.component('font-awesome-layers', FontAwesomeLayers);
Vue.component('font-awesome-layers-text', FontAwesomeLayersText);

axios.defaults.baseURL = 'http://localhost:8000';

axios.interceptors.response.use((response) => response,
  (error) => {
    if (error.response.status === 401) {
      // window.location.href = '/';
      localStorage.clear();
      window.location.href = '/static/home/';
      this.$message.error('请登录！');
    }
  });

axios.interceptors.request.use((config) => {
  // eslint-disable-next-line no-shadow
  const store = JSON.parse(localStorage.getItem('store'));
  if (store !== null) { // 判断是否存在token，如果存在的话，则每个http header都加上token
    const { token } = store;
    // eslint-disable-next-line no-param-reassign
    config.headers.Authorization = `Bearer ${token}`;
  } else {
    // this.$router.push('/');
    window.location.href = '/static/home/';
  }
  return config;
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');

// 全局公共函数
